package com.qingyun.customview.dragview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class MyRawAxisDragView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {
    private var mLastX = 0
    private var mLastY = 0
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val rawX = event.rawX.toInt()
        val rawY = event.rawY.toInt()
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                // 当手指按下时，记录当前的坐标
                mLastX = rawX
                mLastY = rawY
            }

            MotionEvent.ACTION_MOVE -> {
                val offsetX = rawX - mLastX
                val offsetY = rawY - mLastY
                layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)
                mLastX = rawX
                mLastY = rawY
            }
        }
        return true
    }
}